package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.BaseContext_id获取;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.pojo.AddressBook_用户地址;
import com.itheima.reggie.service.AddressBookService;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: cl
 * @Date: 2022/4/7 - 04 - 07
 * @Description:
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    private AddressBookMapper addressBookMapper;


    /**
     * 查询指定用户的全部地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook_用户地址>> list(AddressBook_用户地址 addressBook) {
        addressBook.setUserId(BaseContext_id获取.getCurrentId());
        List<AddressBook_用户地址> list = addressBookService.list(
                Wrappers.<AddressBook_用户地址>lambdaQuery()
                        .eq(addressBook.getUserId() != null, AddressBook_用户地址::getUserId, addressBook.getUserId())
                        .eq(AddressBook_用户地址::getIsDeleted, 0)
                        .orderByDesc(AddressBook_用户地址::getUpdateTime)
        );
        return R.success(list);
    }


    /**
     * 修改默认收货地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook_用户地址> setDefault(@RequestBody AddressBook_用户地址 addressBook) {

        //接过来前端用户地址对象，包含用户id和用户地址id,先把默认状态都设为0再根据地址id设为1
        addressBookService.update(
                Wrappers.<AddressBook_用户地址>lambdaUpdate()
                        .eq(AddressBook_用户地址::getUserId, BaseContext_id获取.getCurrentId())
                        .set(AddressBook_用户地址::getIsDefault, 0)
        );

        //两种方式：

        addressBook.setIsDefault(1);
        //根据地址ip设置默认收货地址
        addressBookService.update(addressBook,
                Wrappers.<AddressBook_用户地址>lambdaUpdate()
                        .eq(AddressBook_用户地址::getId, addressBook.getId())
        );
        return R.success(addressBook);
    }


    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> addressBook(@RequestBody AddressBook_用户地址 addressBook) {
        //先设置地址中得用户id
        addressBook.setUserId(BaseContext_id获取.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("添加成功！");
    }


    /**
     * 通过id查询地址详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook_用户地址> get(@PathVariable Long id) {
        AddressBook_用户地址 addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }


    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("default")
    public R<AddressBook_用户地址> getDefault() {
        LambdaQueryWrapper<AddressBook_用户地址> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook_用户地址::getUserId, BaseContext_id获取.getCurrentId());
        queryWrapper.eq(AddressBook_用户地址::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook_用户地址 addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 通过id修改地址信息
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook_用户地址 addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改信息成功！");
    }

    /**
     * 通过id删除地址(  修改isDelete字段为1)
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {

//        //这是删除
//        addressBookService.remove(
//                Wrappers.<AddressBook_用户地址>lambdaQuery()
//                .in(AddressBook_用户地址::getId,ids)
//        );

        //修改isDelete字段为1
        addressBookService.update(
                Wrappers.<AddressBook_用户地址>lambdaUpdate()
                        .in(AddressBook_用户地址::getId, ids)
                        .set(AddressBook_用户地址::getIsDeleted, 1)
        );
        return R.success("删除成功");
    }
}
